package com.itheima.service.imgl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.mapper.OrdersMapper;
import com.itheima.pojo.*;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;



    @Override
    public void submit(Orders orders) {
        Long userID = BaseContext.getCurrentId();//用户id
        Long addressBookId = orders.getAddressBookId();//地址id
        long orderId = IdWorker.getId();//自动生成id
        User user = userService.getById(userID);//获取用户对象
        AddressBook addressBook = addressBookService.getById(addressBookId);//获取地址对象
        BigDecimal amount = new BigDecimal(0);//设置总金额初始数量
        //获取购物车清单
        QueryWrapper<ShoppingCart> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id",user.getId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrap);

        List<OrderDetail> orderDetailList = new ArrayList<>();//创建订单详情清单集合
        //保存订单详细信息
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
            amount = amount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
            orderDetail.setImage(cart.getImage());
            orderDetailList.add(orderDetail);
        }
        //将上面内容添加入集合中存入内存
        orderDetailService.saveBatch(orderDetailList);
        //保存订单信息
        orders.setNumber(UUID.randomUUID()+"");
        orders.setStatus(2);
        orders.setUserId(userID);
        orders.setAddressBookId(addressBookId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPayMethod(orders.getPayMethod());
        orders.setAmount(amount);
        orders.setRemark(orders.getRemark());
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        this.save(orders);
        //清空购物车
        shoppingCartService.remove(queryWrap);
    }
}
